
package com.be.beadmin.basicData.impl;

import com.be.beadmin.basicData.entity.Workstation;
import com.be.beadmin.basicData.entity.Workstation_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.basicData.mapper.WorkstationMapperEx;
import com.be.beadmin.basicData.repository.WorkstationRepository;
import com.be.beadmin.basicData.service.WorkstationService;
import com.be.beadmin.basicData.dto.input.WorkstationDto;
import com.be.beadmin.basicData.dto.output.WorkstationEx;
import com.be.beadmin.basicData.dto.input.WorkstationQueryCriteria;
import com.be.beadmin.basicData.mapper.WorkstationMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author chenwei
* @date 2023-02-25
**/
@Service
@RequiredArgsConstructor
public class WorkstationServiceImpl implements WorkstationService {

    @Autowired
    private  WorkstationRepository workstationRepository;
    @Autowired
    private  WorkstationMapper workstationMapper;
    @Autowired
    private  WorkstationMapperEx workstationExMapper;

    @Override
    public Page<WorkstationEx> getWorkstationByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<Workstation> page = workstationRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(Workstation_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(Workstation_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<WorkstationEx> page1 = page.map(workstationExMapper::toEntity);
            return page1;

    }

    @Override
    public void addWorkstation(WorkstationDto workstationDto) {
        List<Workstation> plants = workstationRepository.findWorkstationByWorkstationCodeAndDelete(workstationDto.getWorkstationCode(), 0);
        if(ObjectUtil.isNotEmpty(plants) && plants.size()> 0) {
            throw new BeadminException("工作站点编码已经存在,请重新添加");
        }
         Workstation workstation = workstationMapper.toEntity(workstationDto);
         workstationRepository.save(workstation);
    }

    @Override
    public void updateWorkstation(WorkstationDto workstationDto) {

        Workstation result = workstationRepository.findWorkstationByIdAndDelete(workstationDto.getId(), 0);
        if(ObjectUtil.isNotEmpty(result)) {
            throw new BeadminException("Workstation不存在,请重新查询");
        }
        result = workstationMapper.toEntity(workstationDto);
        workstationRepository.save(result);
    }


    @Override
    public void delWorkstation(String id) {
        Workstation workstation = workstationRepository.findWorkstationById(id);
        workstation.setDelete(1);
        workstationRepository.save(workstation);
    }

    @Override
    public void delWorkstations (List<String> ids) { }
}